package com.fingard.dsp.bank.directbank.cwgs01;

import java.util.Date;

import com.fingard.constant.Format;
import com.fingard.dsp.bank.directbank.bankInterface.IBalance;
import com.fingard.dsp.bank.reqrespobj.balance.Req7505HisBalQry;
import com.fingard.dsp.bank.reqrespobj.balance.Req7506BalQry;
import com.fingard.dsp.bank.reqrespobj.balance.RetAts7505HisBalQry;
import com.fingard.dsp.bank.reqrespobj.balance.RetAts7506BalQry;
import com.fingard.dsp.bank.reqrespobj.balance.detail.RetAtsBalDetail;
import com.fingard.text.StringHelper;
import com.fingard.xml.XmlTextReader;
import com.fingard.xml.XmlTextWriter;

/**
 * Created by F on 2019/4/25.
 */
public class CWGS01Balance extends CWGS01Base implements IBalance {
    @Override
    public RetAts7506BalQry d_7506_BalQry(Req7506BalQry p_req7506) throws Exception {
        RetAts7506BalQry retAts7506BalQry = new RetAts7506BalQry(p_req7506.reqHeader);
        retAts7506BalQry.writeStart();
        while (p_req7506.readRecord()) {
            RetAtsBalDetail detail = new RetAtsBalDetail();
            String validCode = "";
            try {
                validCode = login(p_req7506.reqHeader.transType);
                if(StringHelper.isNullOrEmpty(validCode)){
                    throw new Exception("登入失败");
                }
                XmlTextWriter xmlWriter = new XmlTextWriter();
                CWGS01Header header = new CWGS01Header();
                header.batchNo = p_req7506.reqHeader.transSeqID;
                header.bizCode = "8360";
                wirteCommenHeader(xmlWriter, header);
                xmlWriter.writeStartElement("BODY");
//            账号	ACNT_NO	C32		财务公司内部账号
                xmlWriter.writeElementString("ACNT_NO", p_req7506.readedDetail.actNum);
//            查询日期	START_DATE	D	Y	格式YYYY-MM-DD，为空时查询当前余额
                xmlWriter.writeElementString("START_DATE", Format.Date10Format.format(new Date()));
//            用户名	USERNAME	C32		用户名
                xmlWriter.writeElementString("USERNAME", bankFront.loginName);
//            验证码	VALIDATE_CODE	C32		验证码
                xmlWriter.writeElementString("VALIDATE_CODE", validCode);
                xmlWriter.writeEndDocument();
                String[] bankResult = sendToBank(xmlWriter.toXmlString());
                if (bankResult[0].length() == 0) {
                    StringBuilder tmpSbMsg = new StringBuilder();
                    XmlTextReader tmpBkReader = loadBankRespXmlReader(bankResult[1], tmpSbMsg);
                        String retCode = tmpBkReader.getFirstText("/XERP/BODY/RET_CODE");
                        String retMsg = tmpBkReader.getFirstText("/XERP/BODY/RET_MSG");
                        if ("0".equals(retCode)) {
//                        业务返回结果代码	RET_CODE	N		0表示成功，其它为错误码
//                        返回信息描述	RET_MSG	C1024	Y	成功时为空，其它为错误信息
//                        账号	ACNT_NO	C32		财务公司内部账号
                            detail.respCode = "0";
                            detail.respInfo = retMsg;
                            detail.actNum = p_req7506.readedDetail.actNum;
//                        户名	ACNT_NAME	C80		财务公司内部户名
//                        币种	CURRENCY	C3		人民币CNY
                            detail.actCur = convertCurToATS(p_req7506.readedDetail.actCur);
//                        账户余额	BALANCE	N15.2
                            detail.currBal = tmpBkReader.getFirstText("/XERP/BODY/BALANCE");
//                        查询日期	QRY_DATE	D		YYYY-MM-DD
                        } else {
                            throw new Exception(retCode + ":" + retMsg);
                        }
                } else {
                    throw new Exception(bankResult[1]);
                }
            } catch (Exception e) {
                e.printStackTrace();
                detail.respCode = "-1";
                detail.respInfo = e.getMessage();
            } finally {
                logout(validCode,p_req7506.reqHeader.transType);
            }
            retAts7506BalQry.writeRetDetail(detail, p_req7506.isBatch);
        }
        retAts7506BalQry.writeEnd();
        return retAts7506BalQry;
    }


    @Override
    public RetAts7505HisBalQry d_7505_BalQry(Req7505HisBalQry p_req7505) throws Exception {
        return null;
    }
}
